package com.example.demo.service;

import com.example.demo.domain.Feedback;
import com.example.demo.domain.repository.FeedbackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: demo
 * @description: 用户反馈的服务类
 * @author: wangze
 * @create: 2020-04-26 17:50
 **/
@Service
public class FeedbackService {
    @Autowired
    private FeedbackRepository feedbackRepository;

    /**
    * @Description: 获取未处理反馈列表
    * @Param:  无
    * @return:  未处理反馈列表
    * @Author: wangze
    */
    public List<Feedback> findUnresolvedFeedback(String keyword){
        return feedbackRepository.findByStateIsFalseAndWorkNumberContainingOrStateIsFalseAndTextContainingOrderByDateAsc(keyword,keyword);
    }

    /**
     * @Description: 根据工号搜索未处理反馈
     * @Param:  workNumber: 工号关键字
     * @return:  未处理反馈列表
     * @Author: wangze
     */
    public List<Feedback> findUnresolvedFeedbackByWorkNumber(String workNumber){
        return feedbackRepository.findByStateIsFalseAndWorkNumberContainingOrderByDateAsc(workNumber);
    }

    /**
     * @Description: 根据反馈内容搜索未处理反馈
     * @Param:  text: 反馈内容关键字
     * @return:  未处理反馈列表
     * @Author: wangze
     */
    public List<Feedback> findUnresolvedFeedbackByText(String text){
        return feedbackRepository.findByStateIsFalseAndTextContainingOrderByDateAsc(text);
    }

    /**
    * @Description: 获取已处理反馈列表
    * @Param:  无
    * @return:  已处理反馈列表
    * @Author: wangze
    */
    public List<Feedback> findResolvedFeedback(String keyword){
        return feedbackRepository.findByStateIsTrueAndWorkNumberContainingOrStateIsTrueAndTextContainingOrderByDateAsc(keyword,keyword);
    }

    /**
     * @Description: 根据工号搜索已处理反馈
     * @Param:  workNumber: 工号关键字
     * @return:  已处理反馈列表
     * @Author: wangze
     */
    public List<Feedback> findResolvedFeedbackByWorkNumber(String workNumber){
        return feedbackRepository.findByStateIsTrueAndWorkNumberContainingOrderByDateAsc(workNumber);
    }

    /**
     * @Description: 根据反馈内容搜索已处理反馈
     * @Param:  text: 反馈内容关键字
     * @return:  已处理反馈列表
     * @Author: wangze
     */
    public List<Feedback> findResolvedFeedbackByText(String text){
        return feedbackRepository.findByStateIsTrueAndTextContainingOrderByDateAsc(text);
    }

    /**
     * @Description:  增加用户反馈
     * @Param:  ticket 票据实体
     * @return:  添加结果
     * @Author: wangze
     */
    public Feedback addFeedback(Feedback feedback) {
        feedback.setDate(new Date());
        feedback.setState(false);
        return feedbackRepository.save(feedback);
    }

    /**
     * @Description: 删除反馈
     * @Param:  id: 反馈ID
     * @return:  无
     * @Author: wangze
     */
    public void deleteFeedback(Integer id){
        feedbackRepository.deleteFeedbackById(id);
    }

    /**
     * @Description: 设置反馈状态为已解决
     * @Param:  id: 反馈ID
     * @return:  反馈实体
     * @Author: wangze
     */
    public Feedback resetFeedbackState(Integer id){
        Feedback feedback = feedbackRepository.findFeedbackById(id);
        feedback.setState(true);
        return feedbackRepository.save(feedback);
    }

    /**
     * @Description: 获取反馈详情
     * @Param:  id: 反馈ID
     * @return:  反馈实体
     * @Author: wangze
     */
    public Feedback getFeedback(Integer id){
        return feedbackRepository.findFeedbackById(id);
    }

}
